<?php

namespace backend\controllers;

use app\models\AsrCallClassify;
use backend\models\ResetPasswordForm;
use common\models\SignupForm;
use common\models\SiteloginForm;
use function json_encode;
use function var_dump;
use Yii;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;

/**
 * Site controller
 */
class SiteController extends CommonController
{
    /**
     *
     * {@inheritdoc}
     *
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => [
                            'login',
                            'signup',
                            'error'
                        ],
                        'allow' => true
                    ],
                    [
                        'actions' => [
                            'logout',
                            'index'
                        ],
                        'allow' => true,
                        'roles' => [
                            '@'
                        ]
                    ]
                ]
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => [
                        'post'
                    ]
                ]
            ]
        ];
    }

    /**
     *
     * {@inheritdoc}
     *
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction'
            ]
        ];
    }

    /**
     * Displays homepage.
     * 登录
     * @return string
     */
    public function actionIndex()
    {
        $start = strtotime(date('Y-m-d', time()));
        $end = $start + 86400;
        //今日拨打
        $todayall = AsrCallClassify::find()->where(['>=','created_at',$start])->andWhere(['<','created_at',$end])->count();
        //今日接听
        $todayanwer = AsrCallClassify::find()->where(['is_answer'=>1])->andWhere(['>=','created_at',$start])->andWhere(['<','created_at',$end])->count();

        //今日未接通
        $todaynotanwer = AsrCallClassify::find()->where(['is_answer'=>0])->andWhere(['>=','created_at',$start])->andWhere(['<','created_at',$end])->count();
        //今日接听率
        if ($todayall ==0 ){
            $todayratio = "0%";
        }else{
            $todayratio = round($todayanwer/$todayall*100,2) ."%";
        }
        //前1天
        $todayanwer1 = AsrCallClassify::find()->where(['>=', 'created_at', $start - 86400])->andWhere(['<', 'created_at', $start])->andWhere(['=', 'is_answer', 1])->count();
//        echo date('Y-m-d H:i:s',strtotime(date('Y-m-d',strtotime('-2 day'))));
//        exit;
        //前2天
        $todayanwer2 = AsrCallClassify::find()->where(['>=', 'created_at', strtotime(date('Y-m-d', strtotime('-2 day')))])->andWhere(['<', 'created_at', strtotime(date('Y-m-d', strtotime('-1 day')))])->andWhere(['=', 'is_answer', 1])->count();
        //前3天
        $todayanwer3 = AsrCallClassify::find()->where(['>=', 'created_at', strtotime(date('Y-m-d', strtotime('-3 day')))])->andWhere(['<', 'created_at', strtotime(date('Y-m-d', strtotime('-2 day')))])->andWhere(['=', 'is_answer', 1])->count();
        //前4天
        $todayanwer4 = AsrCallClassify::find()->where(['>=', 'created_at', strtotime(date('Y-m-d', strtotime('-4 day')))])->andWhere(['<', 'created_at', strtotime(date('Y-m-d', strtotime('-3 day')))])->andWhere(['=', 'is_answer', 1])->count();
        //前5天
        $todayanwer5 = AsrCallClassify::find()->where(['>=', 'created_at', strtotime(date('Y-m-d', strtotime('-5 day')))])->andWhere(['<', 'created_at', strtotime(date('Y-m-d', strtotime('-4 day')))])->andWhere(['=', 'is_answer', 1])->count();
        //前6天
        $todayanwer6 = AsrCallClassify::find()->where(['>=', 'created_at', strtotime(date('Y-m-d', strtotime('-6 day')))])->andWhere(['<', 'created_at', strtotime(date('Y-m-d', strtotime('-5 day')))])->andWhere(['=', 'is_answer', 1])->count();

        //没问题
        //前1天
        $todaynotanwer1 = AsrCallClassify::find()->where(['>=', 'created_at', $start - 86400])->andWhere(['<', 'created_at', $start])->andWhere(['=', 'is_answer', 0])->count();
        //前2天
        $todaynotanwer2 = AsrCallClassify::find()->where(['>=', 'created_at', strtotime(date('Y-m-d', strtotime('-2 day')))])->andWhere(['<', 'created_at', strtotime(date('Y-m-d', strtotime('-1 day')))])->andWhere(['=', 'is_answer', 0])->count();
        //前3天
        $todaynotanwer3 = AsrCallClassify::find()->where(['>=', 'created_at', strtotime(date('Y-m-d', strtotime('-3 day')))])->andWhere(['<', 'created_at', strtotime(date('Y-m-d', strtotime('-2 day')))])->andWhere(['=', 'is_answer', 0])->count();
        //前4天
        $todaynotanwer4 = AsrCallClassify::find()->where(['>=', 'created_at', strtotime(date('Y-m-d', strtotime('-4 day')))])->andWhere(['<', 'created_at', strtotime(date('Y-m-d', strtotime('-3 day')))])->andWhere(['=', 'is_answer', 0])->count();
        //前5天
        $todaynotanwer5 = AsrCallClassify::find()->where(['>=', 'created_at', strtotime(date('Y-m-d', strtotime('-5 day')))])->andWhere(['<', 'created_at', strtotime(date('Y-m-d', strtotime('-4 day')))])->andWhere(['=', 'is_answer', 0])->count();
        //前6天
        $todaynotanwer6 = AsrCallClassify::find()->where(['>=', 'created_at', strtotime(date('Y-m-d', strtotime('-6 day')))])->andWhere(['<', 'created_at', strtotime(date('Y-m-d', strtotime('-5 day')))])->andWhere(['=', 'is_answer', 0])->count();


        return $this->render('index',['todayall'=>$todayall,'todayratio'=>$todayratio,'todayanwer'=>$todayanwer,'todaynotanwer'=>$todaynotanwer,'todayanwer1'=>$todayanwer1,'todayanwer2'=>$todayanwer2,'todayanwer3'=>$todayanwer3,'todayanwer4'=>$todayanwer4,'todayanwer5'=>$todayanwer5,'todayanwer6'=>$todayanwer6,'todaynotanwer1'=>$todaynotanwer1,'todaynotanwer2'=>$todaynotanwer2,'todaynotanwer3'=>$todaynotanwer3,'todaynotanwer4'=>$todaynotanwer4,'todaynotanwer5'=>$todaynotanwer5,'todaynotanwer6'=>$todaynotanwer6]);
    }

    /**
     * Signs user up.
     *
     * @return mixed
     */
    public function actionSignup()
    {
        $model = new SignupForm();
        if ($model->load(Yii::$app->request->post())) {
            if ($user = $model->signup()) {
                if (Yii::$app->getUser()->login($user)) {
                    Yii::$app->session->setFlash('success', '注册用户成功');
                    return $this->goHome();
                }
            }
        }

        return $this->render('signup', [
            'model' => $model,
        ]);
    }

    /**
     * Login action.
     *
     * @return string
     */
    public function actionLogin()
    {
        if (!Yii::$app->user->isGuest) {
            return $this->goHome();
        }

        $model = new SiteloginForm ();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            return $this->goBack();
        } else {
            $model->password = '';

            return $this->render('login', [
                'model' => $model
            ]);
        }
    }

    /**
     * Logout action.
     *
     * @return string
     */
    public function actionLogout()
    {
        Yii::$app->user->logout();
        //清除cookie以防止不同胡浏览器出现兼容问题无法退出的问题
        Yii::$app->response->cookies->remove('_identity-backend');
        return $this->goHome();
    }

    /**
     * Logout action.
     *
     * @return string
     */
    public function actionUser()
    {
        return $this->render("user");
    }


    /**
     * Resets password.
     *
     * @param string $token
     * @return mixed
     * @throws BadRequestHttpException
     */
    public function actionResetPassword()
    {
        try {
            $passwordResetRequestForm = new \app\models\PasswordResetRequestForm();
            $passwordResetRequestForm->email = Yii::$app->user->identity->email;
            $token =  $passwordResetRequestForm->sendEmail();
            $model = new ResetPasswordForm($token);
        } catch (\HttpInvalidParamException $e) {
            throw new BadRequestHttpException($e->getMessage());
        }

        if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->resetPassword()) {
            Yii::$app->session->setFlash('success', '新密码修改成功');

            return $this->goHome();
        }

        return $this->render('resetPassword', [
            'model' => $model,
        ]);
    }
}
